package com.cater.task.service;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cater.common.BaseEntity;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;
import com.cater.sys.dao.DictionarydataDao;
import com.cater.sys.entity.Dictionarydata;
import com.cater.sys.service.BaseService;
import com.cater.task.dao.TaskSubManagerDao;
import com.cater.task.entity.TaskSubManager;

/**
 * 描述：任务子表 服务实现层
 * 
 * @author yong
 * @date 2019/04/14
 */
@Service
public class TaskSubManagerServiceImpl implements BaseService<TaskSubManager, CommonQuery, String> {

	@Autowired
	TaskSubManagerDao dao;
	@Autowired
	DictionarydataDao dictDao;

	@Override
	public PageResult<TaskSubManager> findPageResult(CommonQuery query) {
		PageResult<TaskSubManager> result = new PageResult<TaskSubManager>();
		result.setPage(query.getPage());
		result.setPageSize(query.getRows());
		int count = dao.findCount(query);
		int first = (query.getPage() - 1) * query.getRows();
		int max = query.getRows();
		List<TaskSubManager> list = dao.findList(query, first, max);
		Dictionarydata dictStatus = null;
		Dictionarydata dictDegree = null;
		for (TaskSubManager taskSubManager : list) {
			String status = taskSubManager.getStatus();
			if (StringUtils.isNotEmpty(status)) {
				dictStatus = new Dictionarydata();
				dictStatus = dictDao.getByTypeValue(status, "task_status");
				if (dictStatus != null) {
					taskSubManager.setDictStatus(dictStatus.getValueName());
				}
			}
			String degree = taskSubManager.getUrgency();
			if (StringUtils.isNotEmpty(degree)) {
				dictDegree = new Dictionarydata();
				dictDegree = dictDao.getByTypeValue(degree, "task_urgency");
				if (dictDegree != null) {
					taskSubManager.setDictDegree(dictDegree.getValueName());
				}
			}
		}
		result.setRows(list);
		result.setRecords(count);
		return result;
	}

	@Override
	public List<TaskSubManager> getAll() {
		return dao.getAll();
	}

	@Override
	public List<TaskSubManager> getAllCommon(CommonQuery query) {
		return dao.getAllCommon(query);
	}

	@Override
	public int save(TaskSubManager t) {
		return 0;
	}

	@Override
	public String saves(TaskSubManager t) {
		t.setId(UUID.randomUUID().toString().replaceAll("-", ""));
		BaseEntity.getCreate(t);
		dao.save(t);
		return t.getId();
	}

	@Override
	public void update(TaskSubManager t) {
		BaseEntity.getupdate(t);
		dao.update(t);
	}

	@Override
	public void delete(String id) {
		dao.delete(id);
	}

	@Override
	public TaskSubManager getById(String id) {
		return dao.getById(id);
	}
}